/*
 * MainWindow.cpp
 *
 * Copyright 2014 raffaele <raffaele@casa>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 *
 *
 */

#include "MainWindow.hpp"

MainWindow::MainWindow(QMainWindow *parent) : QMainWindow(parent)
{
	setupUi(this);
	this->setFixedSize(this->geometry().width(),this->geometry().height());
	sys = 0;
	ret = 0;
}

void MainWindow::pro()
{
	messageBox(".pro file","would you create the file.pro");
	if(ret == QMessageBox::Yes)
	{
		bool ok;
		QString file = QInputDialog::getText(this, tr("Edit your project file name"),tr("project file name without \".pro\":"), QLineEdit::Normal,"", &ok);
		QString name = QInputDialog::getText(this, tr("Edit your file name"),tr("file name:"), QLineEdit::Normal,"", &ok);

		sys = system( "qmake  -project -o temp.pro" );

		string proName;	//string of the file name
		proName = file.toStdString() + ".pro";	//convert QString to string to open the file

		vector<string> add;
		add.push_back("CONFIG += qt");
		add.push_back("QT += core");
		add.push_back("QT += gui");
		add.push_back("QT += widgets");
		string t = "TARGET = " + name.toStdString();
		add.push_back(t.c_str());
		finsert(add);

		vector<int> array;
		array.push_back(5);
		array.push_back(6);
		array.push_back(7);
		array.push_back(10);
		array.push_back(11);
		fline (proName,array);
	}
}

void MainWindow::qmake()
{
	messageBox("qmake","would you create the Makefile?");
	if (ret == QMessageBox::Yes)
	{
		sys = system("qmake");
	}
}

void MainWindow::make()
{
	messageBox("compile","would you compile the project?");
	if (ret == QMessageBox::Yes)
	{
			sys = system("make -B");
	}
}

void MainWindow::messageBox(QString title, QString question)
{
	QMessageBox reply;
	reply.setWindowTitle(title);
	reply.setText(question);
	reply.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
	reply.setDefaultButton(QMessageBox::No);
	ret = reply.exec();
}

void MainWindow::path ()
{

	char *dir = new char;
	cout << get_current_dir_name() << endl;

	QString path = QFileDialog::getExistingDirectory(this, tr("Select Dir"),"/home",
		QFileDialog::ShowDirsOnly
	);
	this->txt_dir->setText(path);
	QByteArray ba = path.toLocal8Bit();
	dir = ba.data();
	
	ret = chdir(dir);
	cout << get_current_dir_name() << endl;

}

void MainWindow::fline (string name,vector<int> exc)
{
	char line[300];//file line
	int n = 0;//line counter
	bool test = false;
	int size = exc.size();
	fstream file;
	file.open("temp2.pro");
	ofstream out;
	out.open(name.c_str());
	while (!file.eof())//if file is not finished
	{
		file.getline(line,300);
		for (int i = 0; i < size ; i++)
		{
			if (n==exc[i] and test == false)
			{
				test = true;
			}
		}
		if (test == false)
		{
			out << line << endl;
		}
		n++;
		test = false;
	}
	file.close();
	out.close();
	remove("temp2.pro");
}

void MainWindow::finsert(vector<string> line)
{
	int size = line.size();
	char l[300];
	fstream a;
	a.open("temp.pro");
	ofstream b;
	b.open("temp2.pro");

	for (int i = 0; i < size; i++)
	{
		b << line[i].c_str() << endl;
	}

	while (!a.eof())//if file is not finished
	{
		a.getline(l,300);
		b << l << endl;
	}
	a.close();
	b.close();
	remove("temp.pro");
}
